import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class CalculationSystemV03 {
    // 可配置：总题数50，加减比例7:3
    private static final int TOTAL = 50;
    private static final int ADD_RATIO = 7;
    private static final int SUB_RATIO = 3;

    // 模块1：按比例生成运算符
    public static char getOpByRatio() {
        return new Random().nextInt(ADD_RATIO+SUB_RATIO) < ADD_RATIO ? '+' : '-';
    }
    // 模块2：生成合法数字（复用v0.2）
    public static int[] getValidNums(char op) {
        Random r = new Random();
        int a, b;
        if (op == '+') {a = r.nextInt(100)+1; b = r.nextInt(100-a+1)+1;}
        else {a = r.nextInt(100)+1; b = r.nextInt(a)+1;}
        return new int[]{a, b};
    }
    // 模块3：生成不重复算式（Set去重）
    public static String[] generateUniqueFormula(Set<String> set) {
        char op; int a, b, ans; String key;
        do {
            op = getOpByRatio();
            int[] nums = getValidNums(op);
            a = nums[0]; b = nums[1]; ans = (op=='+')?a+b:a-b;
            key = a + " " + op + " " + b; // 去重key
        } while (set.contains(key));
        set.add(key);
        return new String[]{key + " = " + ans, String.valueOf(op)};
    }
    // 模块4：统计并输出比例
    public static void printRatio(int add, int sub) {
        System.out.println("\n=== 习题比例统计 ===");
        System.out.println("总题数：" + TOTAL);
        System.out.println("加法：" + add + "道（" + String.format("%.1f", add*100.0/TOTAL) + "%）");
        System.out.println("减法：" + sub + "道（" + String.format("%.1f", sub*100.0/TOTAL) + "%）");
    }
    // 模块5：主函数-整合所有功能
    public static void main(String[] args) {
        System.out.println("=== 加减法口算系统-v0.3（去重+比例控制）===");
        Set<String> set = new HashSet<>();
        int addCount = 0, subCount = 0;
        for (int i = 1; i <= TOTAL; i++) {
            String[] info = generateUniqueFormula(set);
            System.out.printf("%-28s", info[0]);
            // 统计加减题数
            if (info[1].equals("+")) addCount++; else subCount++;
            if (i % 2 == 0) System.out.println();
        }
        printRatio(addCount, subCount); // 输出比例，满足“结果复杂”需求
    }
}